"""
预订数据映射层
"""


class ReservationMapper:
    """预订数据映射 - 调用模型的转换方法"""
    
    def to_dict(self, reservation, include_relations=True):
        """将Reservation模型转换为字典"""
        if not reservation:
            return None
        return reservation.to_dict(include_relations=include_relations)
    
    def to_list(self, reservations, include_relations=True):
        """将Reservation列表转换为字典列表"""
        return [res.to_dict(include_relations=include_relations) for res in reservations]
    
    def from_dict(self, data):
        """从字典提取Reservation数据（用于更新）"""
        # 返回可用于更新模型的字典
        from datetime import datetime
        
        update_data = {}
        if 'guestId' in data:
            update_data['guest_id'] = data['guestId']
        if 'roomId' in data:
            update_data['room_id'] = data['roomId']
        if 'checkIn' in data:
            check_in = data['checkIn']
            if isinstance(check_in, str):
                check_in = datetime.strptime(check_in, '%Y-%m-%d')
            update_data['check_in'] = check_in
        if 'checkOut' in data:
            check_out = data['checkOut']
            if isinstance(check_out, str):
                check_out = datetime.strptime(check_out, '%Y-%m-%d')
            update_data['check_out'] = check_out
        if 'totalPrice' in data:
            update_data['total_price'] = data.get('totalPrice', 0)
        if 'deposit' in data:
            update_data['deposit'] = data.get('deposit', 0)
        if 'status' in data:
            update_data['status'] = data.get('status', 1)
        return update_data
    
    def get_status_text(self, status_code):
        """预订状态码转文本描述（兼容方法，实际使用模型中的STATUS_MAP）"""
        from models import Reservation
        return Reservation.STATUS_MAP.get(status_code, '未知状态')
